package com.zzg.controller;

import java.util.List;

import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzg.entity.Customer;

@Controller
public class TestController {
	@Autowired
	private ElasticsearchTemplate  template;
	
	/**
	 * 
	 * @Title: searchFieldMatchQuery   
	 * @Description: 单字段属性匹配  
	 * @param:       
	 * @return: void      
	 * @throws
	 */
	@RequestMapping(value = "/fieldMatchQuery", method = RequestMethod.GET)
	@ResponseBody
	public void searchFieldMatchQuery(){
		QueryBuilder queryBuilder = QueryBuilders.termQuery("lastName", "Smith");
		SearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(queryBuilder)
				.build();
		System.out.println("查询的语句:" + searchQuery.getQuery().toString());
		List<Customer> list = template.queryForList(searchQuery, Customer.class);
		list.stream().forEach(item->{
			System.out.println(item.toString());
		});
	}
}
